package com.cn.lottery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.lottery.Server.Server;
import com.cn.lottery.domain.LotteryDO;
import com.cn.lottery.domain.PrizeDO;
import com.cn.lottery.domain.TimeSettingDO;

public class GiftData {
	
	private static List<Gift> listData;
	
	private static List<Double> rateList;
	
	private static Map<Integer,Map<Integer,PrizeDO>> prizeMap;
	
	private static int timeIndex = -1;
	
	private static TimeSettingDO timeSetting;
	
	public static int getValueIndex(Double value,Server server){
		int index = -1;
		List<Double> list = getGiftRate(server);
		for(int i=0 ; i<list.size() ; i++){
			if(list.get(i)<value){
				continue;
			}else{
				index = i;
				break;
			}
		}
		return index;
	}
	public static Gift getGift(int idx,Server server){
		List<Gift> list = getGiftList(server);
		if(list == null || list.size() == 0){
			return null;
		}
		return list.get(idx);
	}
	
	public static int getTimeIndex(Server server){
		if(timeIndex == -1){
			initTimeIndex(server);
		}
		return timeIndex;
	}
	
	private static TimeSettingDO getTimeSetting(Server server){
		if(timeSetting == null){
			initTimeIndex(server);
		}
		return timeSetting;
	}
	
	private synchronized static void initTimeIndex(Server server){
		List<TimeSettingDO> timeList = server.searchTimeSetting();
		if(timeList != null && timeList.size()>0){
			timeSetting = timeList.get(0);
			timeIndex = timeSetting.getTimeIndex();
		}
	}
	
	public static List<Gift> getGiftList(Server server){
		if(listData == null){
			initData(server);
		}
		return listData;
	}
	
	private synchronized static void initData(Server server){
		Integer index = getTimeIndex(server);
		initPrize(index,server);
		List<LotteryDO> list = server.searchGiftList(index);
		listData = new ArrayList<Gift>();
		for(LotteryDO lot  : list){
			PrizeDO prize = getPrize(lot.getId(),server);
			listData.add(new Gift(lot.getId(),
					lot.getSequence(),
					lot.getName(),
					lot.getPoolSize(),
					lot.getMaxNum(),
					prize,
					lot.getProbability()));
		}
		Collections.sort(listData);
	}
	
	private static PrizeDO getPrize(Integer lotterId,Server server){
		Integer timeIdx = getTimeIndex(server);
		if(prizeMap == null || prizeMap.get(timeIdx) == null){
			initPrize(timeIdx,server);
		}
		return prizeMap.get(timeIdx).get(lotterId);
	}
	
	private static void initPrize(Integer timeIdx,Server server){
		if(prizeMap == null){
			prizeMap = new HashMap<Integer,Map<Integer,PrizeDO>>();
		}
		if(prizeMap.get(timeIdx) == null){
			List<PrizeDO> list = searchPrize(timeIdx,server);
			Map<Integer,PrizeDO> prizeListMap = new HashMap<Integer,PrizeDO>();
			for(PrizeDO priz :list){
				prizeListMap.put(priz.getLotteryId(), priz);
			}
			prizeMap.put(timeIdx, prizeListMap);
		}
	}
	
	private static synchronized List<PrizeDO> searchPrize(Integer timeIdx,Server server){
		List<PrizeDO> list = server.searchPrize(timeIndex);
		if(list == null || list.size() == 0){
			//创建数据
			server.createPrize(timeIndex);
			list = server.searchPrize(timeIndex);
		}
		return list;
	}
	
	public static List<Double> getGiftRate(Server server){
		if(rateList == null ){
			initRate(server);
		}
		return rateList;
	}
	
	public static void clearn(){
		listData = null;
		rateList = null;
	}
	
	private synchronized static void initRate(Server server){
		List<Gift> list = getGiftList(server);
		double sum = 0d;
		for(Gift gif:list){
			sum += gif.getPool_size();
		}
		double sumTemp = 0d;
		rateList = new ArrayList<Double>();
		for(int i=0 ;i < list.size() ; i++){
			sumTemp += list.get(i).getPool_size();
			double rate = sumTemp/sum;
			rateList.add(rate);
		}
	}
}
